/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created by dnoakes on 12-Nov-2002 22:27:52 using IntelliJ IDEA.
 */

package au.com.lastweekend.jim.io.exif;

import au.com.lastweekend.jim.io.metadata.Directory;
import au.com.lastweekend.jim.io.metadata.MetadataException;
import au.com.lastweekend.jim.io.metadata.AbstractTagDescriptor;
import au.com.lastweekend.jim.util.Rational;

/**
 * 
 * 
 * 
 * @version $Id: GPSDescriptor.java,v 1.3 2006/04/16 09:17:40 ggardner Exp $
 * @author <a href="mailto:grant@lastweekend.com.au"> <Grant Gardner> </a>
 */
public class GPSDescriptor extends AbstractTagDescriptor {

    public GPSDescriptor(Directory directory) {

        super(directory);
    }

    @Override
    public String getDescription(int tagType) throws MetadataException {

        switch (tagType) {
            case GPSDirectory.TAG_GPS_ALTITUDE :
                return getGpsAltitudeDescription();
            case GPSDirectory.TAG_GPS_ALTITUDE_REF :
                return getGpsAltitudeRefDescription();
            case GPSDirectory.TAG_GPS_STATUS :
                return getGpsStatusDescription();
            case GPSDirectory.TAG_GPS_MEASURE_MODE :
                return getGpsMeasureModeDescription();
            case GPSDirectory.TAG_GPS_SPEED_REF :
                return getGpsSpeedRefDescription();
            case GPSDirectory.TAG_GPS_TRACK_REF :
            case GPSDirectory.TAG_GPS_IMG_DIRECTION_REF :
            case GPSDirectory.TAG_GPS_DEST_BEARING_REF :
                return getGpsDirectionReferenceDescription(tagType);
            case GPSDirectory.TAG_GPS_TRACK :
            case GPSDirectory.TAG_GPS_IMG_DIRECTION :
            case GPSDirectory.TAG_GPS_DEST_BEARING :
                return getGpsDirectionDescription(tagType);
            case GPSDirectory.TAG_GPS_DEST_DISTANCE_REF :
                return getGpsDestinationReferenceDescription();
            case GPSDirectory.TAG_GPS_TIME_STAMP :
                return getGpsTimeStampDescription();
                // three rational numbers -- displayed in HH"MM"SS.ss
            case GPSDirectory.TAG_GPS_LONGITUDE :
                return getGpsLongitudeDescription();
            case GPSDirectory.TAG_GPS_LATITUDE :
                return getGpsLatitudeDescription();
            default :
                return getDirectory().getString(tagType);
        }
    }

    private String getGpsLatitudeDescription() throws MetadataException {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_LATITUDE)) {
            return null;
        }
        return getHoursMinutesSecondsDescription(GPSDirectory.TAG_GPS_LATITUDE);
    }

    private String getGpsLongitudeDescription() throws MetadataException {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_LONGITUDE)) {
            return null;
        }
        return getHoursMinutesSecondsDescription(GPSDirectory.TAG_GPS_LONGITUDE);
    }

    private String getHoursMinutesSecondsDescription(int tagType) throws MetadataException {

        Rational[] components = getDirectory().getRationalArray(tagType);
        int deg = components[0].intValue();
        float min = components[1].floatValue();
        float sec = components[2].floatValue();
        // carry fractions of minutes into seconds -- thanks Colin Briton
        sec += (min % 1) * 60;
        return String.valueOf(deg) + "\"" + String.valueOf((int) min) + "'" + String.valueOf(sec);
    }

    private String getGpsTimeStampDescription() throws MetadataException {

        // time in hour, min, sec
        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_TIME_STAMP)) {
            return null;
        }
        int[] timeComponents = getDirectory().getIntArray(GPSDirectory.TAG_GPS_TIME_STAMP);
        StringBuffer sbuffer = new StringBuffer();
        sbuffer.append(timeComponents[0]);
        sbuffer.append(":");
        sbuffer.append(timeComponents[1]);
        sbuffer.append(":");
        sbuffer.append(timeComponents[2]);
        sbuffer.append(" UTC");
        return sbuffer.toString();
    }

    private String getGpsDestinationReferenceDescription() {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_DEST_DISTANCE_REF)) {
            return null;
        }
        String destRef = getDirectory().getString(GPSDirectory.TAG_GPS_DEST_DISTANCE_REF).trim();
        if ("K".equalsIgnoreCase(destRef)) {
            return "kilometers";
        } else if ("M".equalsIgnoreCase(destRef)) {
            return "miles";
        } else if ("N".equalsIgnoreCase(destRef)) {
            return "knots";
        } else {
            return "Unknown (" + destRef + ")";
        }
    }

    private String getGpsDirectionDescription(int tagType) {

        if (!getDirectory().containsTag(tagType)) {
            return null;
        }
        String gpsDirection = getDirectory().getString(tagType).trim();
        return gpsDirection + " degrees";
    }

    private String getGpsDirectionReferenceDescription(int tagType) {

        if (!getDirectory().containsTag(tagType)) {
            return null;
        }
        String gpsDistRef = getDirectory().getString(tagType).trim();
        if ("T".equalsIgnoreCase(gpsDistRef)) {
            return "True direction";
        } else if ("M".equalsIgnoreCase(gpsDistRef)) {
            return "Magnetic direction";
        } else {
            return "Unknown (" + gpsDistRef + ")";
        }
    }

    private String getGpsSpeedRefDescription() {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_SPEED_REF)) {
            return null;
        }
        String gpsSpeedRef = getDirectory().getString(GPSDirectory.TAG_GPS_SPEED_REF).trim();
        if ("K".equalsIgnoreCase(gpsSpeedRef)) {
            return "kph";
        } else if ("M".equalsIgnoreCase(gpsSpeedRef)) {
            return "mph";
        } else if ("N".equalsIgnoreCase(gpsSpeedRef)) {
            return "knots";
        } else {
            return "Unknown (" + gpsSpeedRef + ")";
        }
    }

    private String getGpsMeasureModeDescription() {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_MEASURE_MODE)) {
            return null;
        }
        String gpsSpeedMeasureMode = getDirectory().getString(GPSDirectory.TAG_GPS_MEASURE_MODE).trim();
        if ("2".equalsIgnoreCase(gpsSpeedMeasureMode)) {
            return "2-dimensional measurement";
        } else if ("3".equalsIgnoreCase(gpsSpeedMeasureMode)) {
            return "3-dimensional measurement";
        } else {
            return "Unknown (" + gpsSpeedMeasureMode + ")";
        }
    }

    private String getGpsStatusDescription() {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_STATUS)) {
            return null;
        }
        String gpsStatus = getDirectory().getString(GPSDirectory.TAG_GPS_STATUS).trim();
        if ("A".equalsIgnoreCase(gpsStatus)) {
            return "Measurement in progess";
        } else if ("V".equalsIgnoreCase(gpsStatus)) {
            return "Measurement Interoperability";
        } else {
            return "Unknown (" + gpsStatus + ")";
        }
    }

    private String getGpsAltitudeRefDescription() throws MetadataException {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_ALTITUDE_REF)) {
            return null;
        }
        int alititudeRef = getDirectory().getInt(GPSDirectory.TAG_GPS_ALTITUDE_REF);
        if (alititudeRef == 0) {
            return "Sea level";
        } else {
            return "Unknown (" + alititudeRef + ")";
        }
    }

    private String getGpsAltitudeDescription() throws MetadataException {

        if (!getDirectory().containsTag(GPSDirectory.TAG_GPS_ALTITUDE)) {
            return null;
        }
        String alititude = getDirectory().getRational(GPSDirectory.TAG_GPS_ALTITUDE).toSimpleString(true);
        return alititude + " metres";
    }

    @Override
    public boolean isTagUpdateable(int tagType) {

        return false;
    }
}